﻿using com.uplus.common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApi.Common;
using WebApi.Models;

namespace WebApi.Bill.Biz
{
    public class BizWeather : BizBase
    {


        #region 获取当前天气信息
        public dynamic GetWeatherInfo(string location)
        {
            dynamic  info = new { Temp = $"", Text = "" };
            string sLocat = GetLocationInfo(location);
            if (!string.IsNullOrEmpty(sLocat))
            {
                string url = string.Format("https://devapi.qweather.com/v7/weather/now?location={0}&key={1}&gzip=n", sLocat, HeFengHelper.Weather_Key);
                string res = HttpMgr.GetData(url, "utf-8");
                if (!string.IsNullOrEmpty(res))
                {
                    WeatherDataRoot dataRoot = JsonConvert.DeserializeObject<WeatherDataRoot>(res);
                    if (dataRoot != null && dataRoot.code == "200")
                    {
                        info = new { Temp = $"{dataRoot.now.temp}°", Text = dataRoot.now.text };
                    }
                }
            }
            return info;
        }

        #endregion



        #region 获取7天天气信息
        public dynamic Get7DaysWeatherInfo(string location)
        {
            WeatherResData resData = new WeatherResData();
            List<DaysItem> daysList = new List<DaysItem>();
            WeatherResToday infoToday = new WeatherResToday { SDay = "", STempMax = "", STempMin = "", STextDay = "" };
            DaysItem info = new DaysItem { SWeekDay = $"", STextDay = "", SIconDay="", STempMax = "", STempMin = "", STextNight="", SIconNight="", SDate = "" };
            string sLocat = GetLocationInfo(location);
            if (!string.IsNullOrEmpty(sLocat))
            {
                string url = string.Format("https://devapi.qweather.com/v7/weather/7d?location={0}&key={1}&gzip=n", sLocat, HeFengHelper.Weather_Key);
                //string url = string.Format("https://devapi.qweather.com/v7/weather/3d?location={0}&key={1}&gzip=n", sLocat, HeFengHelper.Weather_Key);
                string res = HttpMgr.GetData(url, "utf-8");
                if (!string.IsNullOrEmpty(res))
                {
                    WeatherDailyDataRoot dataRoot = JsonConvert.DeserializeObject<WeatherDailyDataRoot>(res);
                    if (dataRoot != null && dataRoot.code == "200")
                    {
                        foreach (var item in dataRoot.daily)
                        {
                            if (DateTime.Now.ToString("yyyy-MM-dd").Equals( Convert.ToDateTime(item.fxDate).ToString("yyyy-MM-dd")))
                            {
                                WeatherResToday resToday = new WeatherResToday();
                                resToday.STempMax = $"{item.tempMax}°";
                                resToday.STempMin = $"{item.tempMin}°";
                                if (!string.IsNullOrEmpty(item.textDay)&&item.textDay.Equals(item.textNight))
                                {
                                    resToday.STextDay = item.textDay;
                                }
                                else
                                {
                                    resToday.STextDay = $"{item.textDay}转{item.textNight}";
                                }
                                resToday.SDay = $"{DateTime.Now.ToString("yyyy年MM月dd日")} {ChineseCalendarHelper.GetChineseDateTime(DateTime.Now)}";
                                resData.Today = resToday;
                            }
                            daysList.Add(new DaysItem { SWeekDay = $"{GetDayOfWeek(Convert.ToDateTime(item.fxDate))}", STextDay = item.textDay, SIconDay = item.iconDay, STempMax = $"{item.tempMax}°", STempMin = $"{item.tempMin}°", STextNight = item.textNight, SIconNight = item.iconNight, SDate = Convert.ToDateTime(item.fxDate).ToString("MM/dd") });
                        }
                    }
                    else
                    {
                        daysList.Add(info);
                        resData.Today = infoToday;
                    }
                }
                else
                {
                    daysList.Add(info);
                    resData.Today = infoToday;
                }
            }
            else
            {
                daysList.Add(info);
                resData.Today = infoToday;
            }
            resData.Days = daysList;
            return resData;
        }

        #endregion

        #region 获取位置信息
        public string GetLocationInfo(string location)
        {
            string id = "";
            string url = string.Format("https://geoapi.qweather.com/v2/city/lookup?location={0}&key={1}&gzip=n", location, HeFengHelper.Weather_Key);
            string res = HttpMgr.GetData(url, "utf-8");
            if (!string.IsNullOrEmpty(res))
            {
                GeoDataRoot dataRoot = JsonConvert.DeserializeObject<GeoDataRoot>(res);
                if (dataRoot != null && dataRoot.code == "200")
                {
                    id = dataRoot.location.FirstOrDefault().id;
                }
            }
            return id;
        }
        #endregion


        public string GetDayOfWeek(DateTime date)
        {
            string[] Day = new string[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
            string week = Day[Convert.ToInt32(date.DayOfWeek.ToString("d"))].ToString();
            return  week;
        }

    }
}
